package com.example.mvpdemo.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.mvpdemo.R;
import com.example.mvpdemo.base.BaseActivity;
import com.example.mvpdemo.base.BasePresenter;
import com.example.mvpdemo.login.view.LoginActivity;
import com.example.mvpdemo.utils.Constant;
import com.example.mvpdemo.utils.DensityUtil;
import com.example.mvpdemo.utils.SpUtils;

import java.util.ArrayList;

public class GuideActivity extends AppCompatActivity {
    private static final String TAG = GuideActivity.class.getSimpleName() ;
    private ViewPager view_pager;  //对应的viewPager
    private Button btn_start; //开始休验按钮
    private ArrayList<ImageView> imageviews; //图片数组
    private LinearLayout point_layout; //线形布局
    private ImageView move_point; //移动图标
    private int moveLeftMax; //获得两点之间的间距

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        setContentView(R.layout.activity_guide);
        initView();
        initData();
    }

    public void initView() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        view_pager = findViewById(R.id.view_pager);
        btn_start =  findViewById(R.id.btn_start);
        point_layout = findViewById(R.id.point_layout);
        move_point = findViewById(R.id.move_point);
    }

    public void initData() {
        //准备图片数据
        int[] ids = new int[]{R.drawable.scroll_1,R.drawable.scroll_2,R.drawable.scroll_3};
        imageviews = new ArrayList<>();
        for(int i =0;i < ids.length; i++){
            ImageView img = new ImageView(this);
            img.setImageResource(ids[i]);
            img.setScaleType(ImageView.ScaleType.CENTER_CROP); //设置图片撑满
            imageviews.add(img); //添加图片

            //设置一个点
            ImageView point = new ImageView(this);
            point.setBackgroundResource(R.drawable.yuan_gray);
            int point_width = DensityUtil.dip2px(GuideActivity.this,12); //dp转像素
            int point_height = point_width;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(point_width,point_height);
            if(i!=0){ params.leftMargin = point_width; }
            point.setLayoutParams(params);
            point_layout.addView(point);

        }
        //设置viewpager适配器
        view_pager.setAdapter(new Myviewpager());
        //监听视图渲染完成后
        view_pager.getViewTreeObserver().addOnGlobalLayoutListener(new MyOnGlobalLayoutListener());
        //viewpager监听滑动得到滑动百分比
        view_pager.addOnPageChangeListener(new MyOnPageChangeListener());
        //设置按钮点击事件
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存曾经进入过主页面
                SpUtils.put(Constant.FIRST,1);
                //进入主页页
                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                startActivity(intent);
                //关闭引导页
                finish();
            }
        });

    }

    //viewpager监听改变
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        /**
         * 当页面滚动时回调方法
         * @param position 当前滑动页面位置
         * @param positionOffset 页面滑动百分比
         * @param positionOffsetPixels 滑动的像素
         */
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //两点之间移动的距离 = 屏幕滑动百分比 * 间距
            //int leftmargin = (int) (positionOffset * moveLeftMax);
            //Log.e(TAG,"position=="+position+",positionOffset=="+positionOffset+",positionOffsetPixels=="+positionOffsetPixels);
            //两点间滑动距离对应坐标 = 原来的起始位置 + 两点间移动的距离
            int leftmargin = (int) (position * moveLeftMax + (positionOffset * moveLeftMax));
            //两点间滑动距离对应的坐标
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) move_point.getLayoutParams();
            params.leftMargin = leftmargin;
            move_point.setLayoutParams(params);
        }

        /**
         * 当页面被选中的时候回调的方法
         * @param position 被选中页面对应的位置
         */
        @Override
        public void onPageSelected(int position) {
            //最后一个页面显示按钮
            if(position == imageviews.size()-1){
                btn_start.setVisibility(View.VISIBLE);
            }else{
                btn_start.setVisibility(View.GONE);
            }
        }

        /**
         * 当页面滑动状态发生变化的时候,正在拖，静止，放，停止
         * @param state
         */
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * 创建viewpager适配器
     */
    class Myviewpager extends PagerAdapter {

        /***
         * 返回页面总个数
         * @return
         */
        @Override
        public int getCount() {
            return imageviews.size();
        }

        /**
         * 作用
         * @param container ViewPager
         * @param position 要创建页面的位置
         * @return //返回和创建页面关系的值
         */
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView imageview = imageviews.get(position);
            container.addView(imageview); //添加到容器中
            return imageview;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        /**
         *
         * @param container
         * @param position
         * @param object
         */
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

    }

    /**
     * 注册一个观察者来监听视图树，当视图树的布局、视图树的焦点、视图树将要绘制、
     * 视图树滚动等发生改变时，ViewTreeObserver都会收到通知，
     * ViewTreeObserver不能被实例化，可以调用View.getViewTreeObserver()来获得。     *
     */
    class MyOnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener{

        @Override
        public void onGlobalLayout() {
            //执行不止一次
            move_point.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            //获得两点之间的间距
            moveLeftMax = point_layout.getChildAt(1).getLeft() - point_layout.getChildAt(0).getLeft();
        }
    }
}
